package team.xiaobaiya.gptchatrobot.util;

import java.util.HashMap;
import java.util.Map;


/**
 * @author 程小白
 */
public class Resp<T> {
    private static final int OK_CODE = 0;
    private static final int ERROR_DEFAULT_CODE = -1;
    private static final String ERROR_DEFAULT_MSG = "未知错误，请联系管理员";
    private static final String OK_MSG = "success";

    private Integer code;
    private String msg;
    private T data;

    public Resp(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <R> Resp<R> ok() {
        return new Resp<>(OK_CODE, OK_MSG);
    }

    public static Resp<?>  error() {
        return new Resp<> (ERROR_DEFAULT_CODE, ERROR_DEFAULT_MSG);
    }

    public static <R> Resp<R>  ok(R data) {
        Resp<R>  resp = new Resp<>(OK_CODE, OK_MSG);
        resp.setData(data);
        return resp;
    }
    public static Resp<?>  ok(String msg) {
        return new Resp<>(OK_CODE, msg);
    }

    public static <R> Resp<R>  error(int code, String msg) {
        return new Resp<>(code, msg);
    }

    public static <R> Resp<R>  error(ForCodeEnum codeEnum) {
        return new Resp<>(codeEnum.code(), codeEnum.msg());
    }
    public static <R> Resp<R>  error(ForCodeEnum codeEnum, String extMsg) {
        return new Resp<>(codeEnum.code(), codeEnum.msg() + "," + extMsg);
    }



    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setData(T data) {
        this.data = data;
    }

    public T getData() {
        return data;
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }


    public interface ForCodeEnum {
        /**
         * 获取状态码
         * @return 状态码
         */
        Integer code();

        /**
         * 获取错误信息
         * @return 错误信息
         */
        String msg();
    }



}
